﻿using System;
using System.Collections.Generic;
using System.Text;
using FriendsNearby.Services.Entities;
using InSTEDD.Labs.Geo;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;

namespace FriendsNearby.Services
{
    public class LocationService : ILocationService
    {
        DataAccess da;
        IPeopleNearbyService peoplNearbyService;

        public LocationService(IPeopleNearbyService peoplNearbyService)
        {
            this.peoplNearbyService = peoplNearbyService;
            da = PolicyInjection.Create<DataAccess>();
        }

        #region ILocationService Members

        public void InitializeLocation(string userId)
        {
            da.UpdateUserLocation(userId, null);
        }

        public UserInfo GetCurrentLocation(string userId)
        {
            UserInfo info = new UserInfo();
            info.FacebookUserId = userId;
            info.Location = da.GetCurrentUserLocation(userId);
            return info;
        }

        [Tag("CacheShortTerm")]
        public UserInfo GetLocationAt(string facebookUserId, DateTime pointInTime)
        {
            UserLocationHistory lastLocationChange = da.GetUserLocation(facebookUserId, pointInTime);
            UserInfo info = new UserInfo();
            if (lastLocationChange != null)
            {
                info.FacebookUserId = facebookUserId;
                if (lastLocationChange.Lat.HasValue && lastLocationChange.Long.HasValue)
                {
                    info.Location = new LatLong(lastLocationChange.Lat.Value, lastLocationChange.Long.Value);
                }
                info.LastLocationUpdate = lastLocationChange.Timestamp;
            }
            return info;
        }

        public UserInfo GetLastLocationInfo(string facebookUserId)
        {
            LatLong location;
            DateTime date;
            if (da.GetLastLocationInfo(facebookUserId, out location, out date))
            {
                return new UserInfo() { FacebookUserId = facebookUserId, Location = location, LastLocationUpdate = date };
            }
            else
            {
                return null;
            }
        }
   
        public void SetCurrentLocation(string userId, LatLong location)
        {
            IList<PersonNearby> personNearbyBefore = peoplNearbyService.GetPeopleNearby(userId);
            da.UpdateUserLocation(userId, location);

            IList<PersonNearby> personNearbyAfter = peoplNearbyService.GetPeopleNearby(userId);

            da.AddSocialEntryLog(userId, DateTime.Now, personNearbyBefore != null ? personNearbyBefore.Count : 0, personNearbyAfter!= null? personNearbyAfter.Count : 0);
        }

        public void RemoveLocation(string userId)
        {
            da.RemoveUserLocation(userId);
        }

        public DateTime GetLastNotificationDate(string user)
        {
            return da.GetLastNotificationDate(user);
        }

        public void SaveLastNotificationDate(string user, DateTime dateTime)
        {
            da.SaveLastNotificationDate(user, dateTime);
        }

        public IEnumerable<string> GetMovedUsers(DateTime since)
        {
            return da.GetMovedUsers(since);
        }

        #endregion
    }
}
